<?php

class TicketsController extends Zend_Controller_Action
{
    protected $_form;
    protected $_tickets;
	protected $_tasks;
	protected $_users;

    public function init()
    {

        $this->_form = new Application_Form_Ticket();
        $this->_tasks = new Application_Model_Tasks();
        $this->_tickets = new Application_Model_Tickets();
        $this->_users = new Application_Model_Users();
    }

	public function preDispatch()
    {
        if (!Zend_Auth::getInstance()->hasIdentity()) {
                $this->_helper->redirector('index', 'login');
        }
    }

    public function indexAction()
    {
	    $this->view->title = 'Работни карти';
	    $this->view->headTitle($this->view->title);

	    $this->view->tickets = $this->_tickets->getTicketList();
	    $this->_helper->log('Зареждане на списък с работни карти');

    }

    public function createAction()
    {
        $this->view->title = 'Създаване на работна карта';
        $this->view->headTitle($this->view->title);

        $this->_form->setAction($this->_helper->url('create', 'tickets'));
        $this->view->form = $this->_form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();

            if ($formData['time_end'] < $formData['time_start']) {
				$this->_form->getElement('time_end')->markAsError();
				$this->view->invalid_time = 1;
            }

        	if (strtotime($formData['date_ticket']) == false) {
				$this->_form->getElement('date_ticket')->markAsError();
				$this->view->invalid_date = 1;
            }

            if ($this->_form->isValid($formData)) {
            	$params = $this->_form->getValues();
            	$params["user_id"] = $this->_helper->getLoggedUser();

            	$start = new DateTime($params['date_ticket']." ". $params['time_start']);
        		$time_span = $start->diff(new DateTime($params['date_ticket']." ". $params['time_end']));
       			$hours = $time_span->h;
        		$params["hours"] = $hours;

	            $this->_tickets->createTicket($params);
	            $this->_helper->log("Създаване на работна карта");
	            $this->_helper->redirector('index');
            } else {
                $this->_form->populate($formData);
            }
        }
    }

  public function viewAction()
    {
        $this->view->title = 'Преглед на работна карта';
        $this->view->headTitle($this->view->title);

        $id = $this->_getParam('id', 0);

        try {
            $ticket = $this->_tickets->getTicket($id);
            $user = $this->_users->getUser($ticket['user_id']);
            $task = $this->_tasks->getTask($ticket['task_id']);
            $ticket['user'] = $user['first_name'] . ' ' . $user['last_name'];
  			$ticket['task'] = $task["name"];

            $this->view->ticket = $ticket;
   			$this->_helper->log("Преглед на работна карта $id");
        } catch (Exception $e) {
            $this->_helper->redirector('notfound', 'error');
        }
    }


}